package com.briup.store.web.controller;

import cn.hutool.core.thread.ThreadUtil;
import com.briup.common.response.PageTemplate;
import com.briup.common.response.Result;
import com.briup.store.domain.dto.OrderItemInsertDTO;
import com.briup.store.domain.pojo.Goods;
import com.briup.store.service.GoodsService;
import com.briup.store.domain.vo.GoodsGetDetailVO;
import com.briup.store.domain.vo.GoodsGetHotVO;
import com.briup.store.domain.vo.GoodsGetListVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 获取热销商品
     */
    @GetMapping
    public Result<PageTemplate<GoodsGetHotVO>> getHot(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        PageTemplate<GoodsGetHotVO> goods = goodsService.getHot(pageNum,pageSize);
        return Result.success(goods);
    }

    /**
     * 商品详细信息
     */
    @GetMapping("/{id}")
    public Result getGoodsDetail(@PathVariable Integer id){
        GoodsGetDetailVO goodsGetDetail = goodsService.getGoodsDetail(id);
        return Result.success(goodsGetDetail);
    }

    @ApiOperation("根据id批量查询商品")
    @GetMapping("/batch")
    public List<Goods> queryGoodByIds(@RequestParam("ids") List<Integer> ids){
        return goodsService.queryGoodByIds(ids);
    }

    @GetMapping("/getList")
    public Result<PageTemplate<GoodsGetListVO>> getList(Integer categoryId,@RequestParam Integer pageNum, @RequestParam Integer pageSize){
//        log.info("categoryId: " + categoryId);
//        log.info("pageNum: " + pageNum);
//        log.info("pageSize: " + pageSize);

        //注意: PageTemplate为自定义分页类
        PageTemplate<GoodsGetListVO> list = goodsService.getList(categoryId,pageNum,pageSize);
        return Result.success(list);
    }

    //修改商品库存
    @ApiOperation("批量修改库存")
    @PutMapping("/updateStock")
    public Result updateStock(@RequestBody List<OrderItemInsertDTO> items) {
        goodsService.updateStock(items);
        return Result.success();
    }
}
